package research.ui.editors;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;

import research.domain.Mixer;
import research.domain.Preparation;
import research.entity.EntityType;
import research.model.EntityListContentProvider;
import research.model.ListHierarchy;
import research.persistence.PersistenceManager;
import research.ui.editors.input.EntityEditorInput;

public class PreparationEditor extends FormEntityEditor {
	public static String ID = "research.ui.editors.PreparationEditor";
	private Text name;
	
	private Text speed1;
	private Text temperature1;
	private Text time1;
	private Text description1;
	
	private Text speed2;
	private Text temperature2;
	private Text time2;
	private Text description2;
	
	private Text speed3;
	private Text temperature3;
	private Text time3;
	private Text description3;
	
	private ComboViewer mixer;
	
	private Preparation getEntity() {
		return (Preparation) entity;
	}

	@Override
	protected boolean checkInputEntityType(EntityEditorInput input) {
		return input.getEntity().getType().equals(EntityType.Preparation);
	}

	@Override
	protected void fill() {
		this.name.setText(this.getEntity().getName());
		setComboEntity(mixer, this.getEntity().getMixer());
		
		this.speed1.setText(parseDouble(this.getEntity().getSpeed1()));
		this.temperature1.setText(parseDouble(this.getEntity().getTemperature1()));
		this.time1.setText(parseDouble(this.getEntity().getTime1()));
		this.description1.setText(this.getEntity().getDescription1());
		
		this.speed2.setText(parseDouble(this.getEntity().getSpeed2()));
		this.temperature2.setText(parseDouble(this.getEntity().getTemperature2()));
		this.time2.setText(parseDouble(this.getEntity().getTime2()));
		this.description2.setText(this.getEntity().getDescription2());
		
		this.speed3.setText(parseDouble(this.getEntity().getSpeed3()));
		this.temperature3.setText(parseDouble(this.getEntity().getTemperature3()));
		this.time3.setText(parseDouble(this.getEntity().getTime3()));
		this.description3.setText(this.getEntity().getDescription3());
	}

	@Override
	protected void initialize() {
	}

	@Override
	protected boolean readAndValidate() {
		this.getEntity().setName(this.name.getText());
		
		try {
			this.getEntity().setSpeed1(
					parseString(this.speed1.getText()));
			this.getEntity().setSpeed2(
					parseString(this.speed2.getText()));
			this.getEntity().setSpeed3(
					parseString(this.speed2.getText()));
		} catch (NumberFormatException e) {
			MessageDialog.openWarning(getSite().getShell(), "Error",
					"Ошибка в скорости");
			return false;
		}
		
		try {
			this.getEntity().setTemperature1(
					parseString(this.temperature1.getText()));
			this.getEntity().setTemperature2(
					parseString(this.temperature2.getText()));
			this.getEntity().setTemperature2(
					parseString(this.temperature3.getText()));
		} catch (NumberFormatException e) {
			MessageDialog.openWarning(getSite().getShell(), "Error",
					"Ошибка в температуре");
			return false;
		}
		
		try {
			this.getEntity().setTime1(
					parseString(this.time1.getText()));
			this.getEntity().setTime2(
					parseString(this.time2.getText()));
			this.getEntity().setTime2(
					parseString(this.time3.getText()));
		} catch (NumberFormatException e) {
			MessageDialog.openWarning(getSite().getShell(), "Error",
					"Ошибка во времени");
			return false;
		}
		
		this.getEntity().setDescription1(this.description1.getText());
		this.getEntity().setDescription2(this.description2.getText());
		this.getEntity().setDescription2(this.description3.getText());
		
		Mixer mix = (Mixer) getComboEntity(mixer);
		
		if (mix == null) {
			MessageDialog.openWarning(getSite().getShell(), "Error",
					"Выберите смеситель");
			return false;
		}
		
		this.getEntity().setMixer(mix);

		return validateText(name);
	}

	@Override
	protected void renderForm() {
		TableWrapData td;
		Label label;
		Section section;
		Composite composite;
		GridData gd;
		
		/*
		 * Name
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		section.setLayoutData(td);
		section.setText("Описание");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(2, false));
		section.setClient(composite);
		gd = new GridData();
		label = toolkit.createLabel(composite, "Название*:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		name = toolkit.createText(composite, "",
				SWT.BORDER);
		name.setLayoutData(gd);
		name.addModifyListener(getNameModifyListener());
		
		/*
		 * Mixer
		 */		
		gd = new GridData();
		label = toolkit.createLabel(composite, "Смеситель*:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		mixer = new ComboViewer(composite, SWT.DROP_DOWN
				| SWT.READ_ONLY);
		mixer.setContentProvider(new EntityListContentProvider(new ListHierarchy(EntityType.Mixer), false));
		mixer.setLabelProvider(new LabelProvider());
		mixer.setInput(PersistenceManager.getInstance());
		gd.widthHint = 100;
		mixer.getCombo().setLayoutData(gd);
		mixer.getCombo().addModifyListener(getTextModifyListener());
		
		
		/*
		 * Stage 1
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Этап 1");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(2, false));
		section.setClient(composite);
		gd = new GridData();
		label = toolkit.createLabel(composite, "Скорость, ед.:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		speed1 = toolkit.createText(composite, "",
				SWT.BORDER);
		speed1.setLayoutData(gd);
		speed1.addModifyListener(getTextModifyListener());
		
		/*
		 * Temperature
		 */
		gd = new GridData();
		label = toolkit.createLabel(composite, "Температура, С:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		temperature1 = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		temperature1.setLayoutData(gd);
		temperature1.addModifyListener(getTextModifyListener());
		
		/*
		 * Time 
		 */
		gd = new GridData();
		label = toolkit.createLabel(composite, "Время, мин:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		time1 = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		time1.setLayoutData(gd);
		time1.addModifyListener(getTextModifyListener());
		
		/*
		 * Description 
		 */
		gd = new GridData();
		label = toolkit.createLabel(composite, "Описание:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		description1 = toolkit.createText(composite, "",
				SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		gd.widthHint = 200;
		gd.heightHint = 60;
		description1.setLayoutData(gd);
		description1.addModifyListener(getTextModifyListener());
		
		/*
		 * Stage 2
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Этап 2");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(2, false));
		section.setClient(composite);
		gd = new GridData();
		label = toolkit.createLabel(composite, "Скорость, ед.:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		speed2 = toolkit.createText(composite, "",
				SWT.BORDER);
		speed2.setLayoutData(gd);
		speed2.addModifyListener(getTextModifyListener());
		
		/*
		 * Temperature
		 */
		gd = new GridData();
		label = toolkit.createLabel(composite, "Температура, С:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		temperature2 = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		temperature2.setLayoutData(gd);
		temperature2.addModifyListener(getTextModifyListener());
		
		/*
		 * Time 
		 */
		gd = new GridData();
		label = toolkit.createLabel(composite, "Время, мин:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		time2 = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		time2.setLayoutData(gd);
		time2.addModifyListener(getTextModifyListener());
		
		/*
		 * Description 
		 */
		gd = new GridData();
		label = toolkit.createLabel(composite, "Описание:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		description2 = toolkit.createText(composite, "",
				SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		gd.widthHint = 200;
		gd.heightHint = 60;
		description2.setLayoutData(gd);
		description2.addModifyListener(getTextModifyListener());
		
		/*
		 * Stage 3
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Этап 3");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(2, false));
		section.setClient(composite);
		gd = new GridData();
		label = toolkit.createLabel(composite, "Скорость, ед.:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		speed3 = toolkit.createText(composite, "",
				SWT.BORDER);
		speed3.setLayoutData(gd);
		speed3.addModifyListener(getTextModifyListener());
		
		/*
		 * Temperature
		 */
		gd = new GridData();
		label = toolkit.createLabel(composite, "Температура, С:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		temperature3 = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		temperature3.setLayoutData(gd);
		temperature3.addModifyListener(getTextModifyListener());
		
		/*
		 * Time 
		 */
		gd = new GridData();
		label = toolkit.createLabel(composite, "Время, мин:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		time3 = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		time3.setLayoutData(gd);
		time3.addModifyListener(getTextModifyListener());
		
		/*
		 * Description 
		 */
		gd = new GridData();
		label = toolkit.createLabel(composite, "Описание:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		description3 = toolkit.createText(composite, "",
				SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		gd.widthHint = 200;
		gd.heightHint = 60;
		description3.setLayoutData(gd);
		description3.addModifyListener(getTextModifyListener());

	}

}
